package cn.edu.wzut.controller;


import cn.edu.wzut.mbp.entity.SysDept;
import cn.edu.wzut.mbp.entity.SysRole;
import cn.edu.wzut.mbp.service.ISysDeptService;
import cn.edu.wzut.mbp.service.ISysRoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/dept")
public class DeptController {

    @Autowired
    private ISysDeptService iSysDeptService;

    @GetMapping("/tree")
    public  JsonResult<List<SysDept>> listTree(){
        List<SysDept> list = iSysDeptService.listTree();
        return new JsonResult<>(list);
    }
    //保存与修改
    @PostMapping
    public  JsonResult<Object> save(@RequestBody SysDept sysDept) throws Exception{
        boolean b = iSysDeptService.saveOrUpdate(sysDept);
        if(b)
            return new JsonResult<>(0, "修改完成");
        else
            throw new MyException("修改失败");
    }
   //删除
    @DeleteMapping("/{id}")
    public JsonResult<Object> deleteone(@PathVariable BigDecimal id) throws Exception {
        boolean b = iSysDeptService.removeById(id);
        if(b)
            return new JsonResult<>(0, "删除成功");
        else
            throw new MyException("删除失败");

    }

}
